/**   
 * @Title: XMLReaderAndPaser.java 
 * @Package com.esun.network 
 * @Description: (解析从服务器获取的xml) 
 * @author City Jiangsy@500wan.com   
 * @date 2011-6-2 下午05:06:51  
 * copy right 深圳市易讯天空网络技术有限公司
 */
package com.esun.network;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import com.esun.config.Config;
import com.esun.util.log.EDebug;

/**
 * @ClassName: XMLReaderAndPaser
 * @Description: (工具类 ,解析xml字符串)
 * @author City Jiangsy@500wan.com
 * @date 2011-6-2 下午05:06:51
 * 
 */
public class XMLReaderAndPaser {
	private Hashtable<String, String> tableResult;
	private ArrayList<Hashtable<String, String>> listResult;
	private String tagName;
	private boolean is_list = false;
	private String ERROR = "Paser error..";
	private String TAG_NAME = "tagName = ";
	private String VALUE = ",value = ";

	public XMLReaderAndPaser(String strXML) {
		EDebug.o(this, Config.OPTIONIS_START);

		// 对获取的xml字段转化为输入源
		StringReader readStrXml = new StringReader(strXML);
		InputSource source = new InputSource(readStrXml);

		// 初始化作为返回结果的hashtable
		tableResult = new Hashtable<String, String>();
		// 初始化作为返回结果的ArrayList
		listResult = new ArrayList<Hashtable<String, String>>();

		try {
			// 使用SAX和DefaltHandler工具解析xml字段
			XMLToolHandler xmlToolHandler = new XMLToolHandler();
			SAXParserFactory saxFactory = SAXParserFactory.newInstance();

			XMLReader xmlReader = saxFactory.newSAXParser().getXMLReader();
			xmlReader.setContentHandler(xmlToolHandler);
			xmlReader.parse(source);
			EDebug.o(this, Config.OPTIONIS_END);

		} catch (Exception e) {
			EDebug.o(XMLReaderAndPaser.this, ERROR + e.toString());
			EDebug.o(this, Config.OPTIONIS_END);
		}
	}

	/**
	 * @ClassName: XMLToolHandler
	 * @Description:(DefaultHandler工具)
	 * @author City Jiangsy@500wan.com
	 * @date 2011-6-7 下午12:10:00
	 * 
	 */
	class XMLToolHandler extends DefaultHandler {
		private boolean is_root = false;

		public void startDocument() throws SAXException {

		}

		public void endDocument() throws SAXException {

		}

		// 判定是否为根节点root,如果是,解析开始
		public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
			if (localName.equals(Config.PASER_ROOT)) {
				EDebug.o(this, Config.CRYSTRING + localName);
				this.is_root = true;
			} else if (localName.equals(Config.PASER_ROW)) {
				Hashtable<String, String> rowTable = new Hashtable<String, String>();
				for (int i = 0; i < atts.getLength(); i++) {
					rowTable.put(atts.getLocalName(i), atts.getValue(i));
					// EDebug.o(XMLReaderAndPaser.this,
					// "key= ["+atts.getLocalName(i)+"] value= "+atts.getValue(i));
				}
				listResult.add(rowTable);
			} else if (localName.equals(Config.PASER_LIST)) {
				is_list = true;
			}
			tagName = localName;

		}

		// 判定根节点是否结束
		public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
			if (localName.equals(Config.PASER_ROOT)) {
				EDebug.o(this, Config.CRYSTRING + localName);
				this.is_root = false;
			}
		}

		// 在hashtable中写入子节点数据
		public void characters(char ch[], int start, int length) {
			if (this.is_root) {
				String strValue = new String(ch, start, length);

				// 诡异的判断...存在回车和空格的情况下子节点会出现3次
				if (strValue.indexOf(" ") != 0 && strValue.indexOf("\n") != 0 && strValue.indexOf("\t") != 0) {
					tableResult.put(tagName, strValue);
					EDebug.o(this, TAG_NAME + tagName + VALUE + strValue);
					EDebug.o(this, strValue.indexOf(" ") + Config.ONESPACE + strValue.indexOf("\n") + Config.ONESPACE + strValue.indexOf("\t"));
				}
			}
		}

	}

	/**
	 * @Title: getResult
	 * @Description: (返回数据)
	 * @param @return tableResult
	 * @return Hashtable<String,String> 返回类型
	 * @throws
	 */
	public Hashtable<String, String> getResult() {
		return tableResult;
	}

	public ArrayList<Hashtable<String, String>> getListResult() {
		if (is_list) {
			EDebug.o(is_list + Config.CRYSTRING);
			return listResult;
		} else {
			EDebug.o(!is_list + Config.CRYSTRING);
			Hashtable<String, String> initTable = new Hashtable<String, String>();
			initTable.put(Config.NUM0, Config.NUM0);
			listResult.add(initTable);
			return listResult;
		}
	}
}
